﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using Benefit.Models.Sys;
namespace Benefit.Service.Month
{
    /// <summary>
    /// 经纪公司交易信息
    /// </summary>
    public class ServiceBrokerageMonthTradeInfo : Benefit.Interface.Month.IBrokerageMonthTradeInfo
    {
        Benefit.DB.DBManager db = null;
        public ServiceBrokerageMonthTradeInfo(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 删除当月所有记录
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int month, int year)
        {
            var query = from t in db.BrokerageMonthTradeInfo where t.Month == month && t.Year == year select t;
            foreach (Models.Month.BrokerageMonthTradeInfo d in query)
            {
                db.BrokerageMonthTradeInfo.Remove(d);
            }
            db.SaveChanges();
        }


        /// <summary>
        /// 初始化经纪公司月收益情况
        /// </summary>
        /// <param name="month"></param>
        /// <param name="year"></param>
        public void InitBrokerMonthTradeInfo(int month, int year)
        {
            Models.Month.BrokerageMonthTradeInfo binfo = new Models.Month.BrokerageMonthTradeInfo();
            var query = db.BrokerageDayTradeInfo.Where(a => a.TradeHistory.YPdate == year).Where(a => a.TradeHistory.MPdate == month);
            var query2 = db.BrokerageMonthTradeInfo.Where(a => a.Year == year).Where(a => a.Month == month);
            if (query2.Count() > 0)
            {
                binfo = query2.First();
            }
            binfo.BillCount = query.Sum(a => a.BillCount);
            binfo.BrokerId = query.First().BrokerId;
            binfo.DayCount = query.Sum(a => a.DayCount);
            binfo.Free = query.Sum(a => a.Free);
            binfo.LoseCount = query.Sum(a => a.LoseCount);
            binfo.Month = month;
            binfo.Profit = query.Sum(a => a.Profit);
            binfo.SumLost = query.Sum(a => a.SumLost);
            binfo.SumWin = query.Sum(a => a.SumWin);
            binfo.UsedMargin = query.Sum(a => a.UsedMargin);
            binfo.WinCount = query.Sum(a => a.WinCount);
            binfo.Year = year;
            if (query2.Count() == 0)
            {
                db.BrokerageMonthTradeInfo.Add(binfo);
            }
            db.SaveChanges();
        }

        public List<Models.Month.BrokerageMonthTradeInfo> GetBrokerageMonthTradeInfo(int brokerid, int year, int month)
        {
            var query = db.BrokerageMonthTradeInfo.Where(a => a.BrokerId == brokerid).Where(a => a.Year == year).Where(a => a.Month == month).Include(a => a.Broker);
            return query.ToList();
        }

        public List<Models.Month.BrokerageMonthTradeInfo> GetBrokerageMonthTradeInfo(int year, int month)
        {
            var query = db.BrokerageMonthTradeInfo.Where(a => a.Year == year).Where(a => a.Month == month).Include(a => a.Broker);
            return query.ToList();
        }
    }
}